package com.inetools.gwt.gajaxs.client;

import java.util.LinkedHashSet;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.search.client.Result;
import com.google.gwt.search.client.Search;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Tree;
import com.google.gwt.user.client.ui.TreeItem;
import com.google.gwt.user.client.ui.Widget;
import com.inetools.gwetons.client.AttentionTracker;
import com.inetools.gwetons.client.AttentionTracker.AttentionTrackerListener;

public abstract class SearchResultsViewer implements SearchResultsControllerListener {
	// For attention handling ---------------------------------------------------------------------
	protected LinkedHashSet<SearchResultViewerStateListener> searchResultViewerStateListenerLinkedHashSet = 
		new LinkedHashSet<SearchResultViewerStateListener>();
	public boolean addSearchResultViewerStateListener( SearchResultViewerStateListener searchResultViewerStateListener) {
		if ( searchResultViewerStateListener == null) return false;
		return searchResultViewerStateListenerLinkedHashSet.add( searchResultViewerStateListener);
	}
	public boolean removeSearchResultViewerStateListener( SearchResultViewerStateListener searchResultViewerStateListener) {
		return searchResultViewerStateListenerLinkedHashSet.remove( searchResultViewerStateListener);
	}
	protected void notifySearchResultViewerReadyState() {
GWT.log( "Viewer is ready", null);
		for( SearchResultViewerStateListener searchResultViewerStateListener : searchResultViewerStateListenerLinkedHashSet) {
			searchResultViewerStateListener.viewerReady();
		} // for
	}
	protected void notifySearchResultViewerBusyState() {
GWT.log( "Viewer is busy", null);
		for( SearchResultViewerStateListener searchResultViewerStateListener : searchResultViewerStateListenerLinkedHashSet) {
			searchResultViewerStateListener.viewerBusy();
		} // for
	}

	protected AttentionTracker attentionTracker = new AttentionTracker();
	{
		attentionTracker.addAttentionTrackerListener( new AttentionTrackerListener() {
			@Override
			public void gotAttention() {
				notifySearchResultViewerBusyState();
			}
			@Override
			public void lostAttention() {
				notifySearchResultViewerReadyState();				
			}
		});
	}
	// --------------------------------------------------------------------------------------------

	// About branding logo ------------------------------------------------------------------------
	protected com.google.gwt.user.client.ui.Image googleBrandingImage = 
		new com.google.gwt.user.client.ui.Image( "http://www.google.com/uds/css/small-logo.png");
	{
		Style googleBrandingImageStyle = googleBrandingImage.getElement().getStyle();
		googleBrandingImageStyle.setProperty( "display", "inline");
		googleBrandingImageStyle.setPropertyPx( "paddingTop", 1);
	};
	protected HTML googleBrandingTextHTML = new HTML( "powered by");
	{
		Style googleBrandingTextStyle= googleBrandingTextHTML.getElement().getStyle();
		googleBrandingTextStyle.setProperty( "display", "inline");
		googleBrandingTextStyle.setPropertyPx( "fontSize", 11);
		googleBrandingTextStyle.setProperty( "color", "#676767");
		googleBrandingTextStyle.setPropertyPx( "marginRight", 2);
		googleBrandingTextStyle.setPropertyPx( "paddingBottom", 2);
		googleBrandingTextStyle.setProperty( "textAlign", "right");
		googleBrandingTextStyle.setProperty( "verticalAlign", "top");
	};
	protected HTML googleBrandingHTML = new HTML();
	{
		Element googleBrandingHTMLElement = googleBrandingHTML.getElement();
		googleBrandingHTMLElement.appendChild( googleBrandingTextHTML.getElement());
		googleBrandingHTMLElement.appendChild( googleBrandingImage.getElement());
		googleBrandingHTMLElement.getStyle().setProperty( "float", "right");
	};

//TODO add my branding logo too addition to Google one
	// --------------------------------------------------------------------------------------------

	// --------------------------------------------------------------------------------------------
	public class TreeSelectionHandler implements SelectionHandler<TreeItem> {
		@Override
		public void onSelection(SelectionEvent<TreeItem> event) {
			// event.getSelectedItem().getElement().getStyle().setProperty(
			// "display", "block");
		
			TreeItem treeItemObj = event.getSelectedItem();
			Tree treeObj = treeItemObj.getTree();
			boolean selectionIsItemFlag = false;
			for (int treeItemIndex = 0; treeItemIndex < treeObj.getItemCount(); treeItemIndex++) {
				if (treeItemObj == treeObj.getItem(treeItemIndex)) {
					selectionIsItemFlag = true;
					// treeItemObj.getElement().getStyle().setProperty(
					// "display", "block");
					NodeList<Element> divElementNodeList = treeItemObj
							.getElement().getElementsByTagName("div");
					if (divElementNodeList.getLength() < 1)
						return;
					divElementNodeList.getItem(0).getStyle().setProperty(
							"display", "block");
				}
			} // for
			if (!selectionIsItemFlag) { // Most likely it's item's item
				treeItemObj.getElement().getFirstChildElement().getStyle()
						.setProperty("display", "block");
			}
		}
	} // protected class TreeSelectionHandler implements SelectionHandler<TreeItem>
	// --------------------------------------------------------------------------------------------

	
	// Methods for SearchResultsControllerListener ------------------------------------------------	
	@Override
	public abstract void onNewSearch(); 

	@Override
	public abstract void onSearchResultStackCleared();

	@Override
	public abstract void onSearchResultStackFilled(
			Search search, 
			LinkedHashSet<Result> searchResultStackLinkedHashSet,
			LinkedHashSet<Widget> attributionWidgetLinkedHashSet);
	// --------------------------------------------------------------------------------------------
}
